
*************** This is the first read subroutine. ********************************;
*************** Change only the file name in the first infile statement. **********;
*************** DO NOT change the file name in the second infile statement. *******;
***********************************************************************************;

*************** This is the first read subroutine. ********************************;
*************** Change the path and file name in the infile statement. *;
***********************************************************************************;

data one;

infile 'C:\Users\tebert\Desktop\EPG SAS Manuscript\Alberto Data\Five Aphids\Data\PV2-ch1.ana' dsd dlm='09'x truncover;
input @; _infile_=compress(translate(_infile_,'.',','),'"'); input a b c ;

data one; set one; drop c; insect1=0;dur=0;
data one; set one;
retain holder1 in0;
if in0 ne insect1 then do; holder1=0; in0=insect1; dur=b; end;
else dur=b-holder1;
holder1=b;
data two; set one;
insectno="a1";
waveform=a;
duration=dur;
data two; set two;
drop a b holder1 in0 dur insect1;
data two; set two;
retain holder1 in0;
if in0 ne insectno then do; in0=insectno; holder1=0; end;
else wave1=holder1;
holder1=waveform;
data two; set two; if wave1 ne "." then output;
data two; set two; waveform=wave1;
data two; set two; drop in0 wave1 holder1;
data AllSets; set two;
proc datasets nolist nodetails; delete one two;


*************** This is the generic read subroutine. *******************************;
*************** Change the path and file name in the first infile statement. *******;
*************** Also increment the variable insect1 for each new file.  ************;
************************************************************************************;

data one;
infile 'C:\Users\tebert\Desktop\EPG SAS Manuscript\Alberto Data\Five Aphids\Data\PV2-ch2.ana' dsd dlm='09'x truncover;
input @; _infile_=compress(translate(_infile_,'.',','),'"'); input a b c ;

data one; set one; drop c; dur=0;                           insect1="a2";        
data one; set one; retain holder1 in0;
if in0 ne insect1 then do; holder1=0; in0=insect1; dur=b; end; else dur=b-holder1; holder1=b;
data two; set one; insectno=insect1; waveform=a; duration=dur;
data two; set two; drop a b holder1 in0 dur insect1;
data two; set two; retain holder1 in0;
if in0 ne insectno then do; in0=insectno; holder1=0; end; else wave1=holder1; holder1=waveform;
data two; set two; if wave1 ne "." then output; data two; set two; waveform=wave1;
data two; set two; drop in0 wave1 holder1; data two; set two; proc append base=allsets data=two;
proc datasets nolist nodetails; delete one two;

data one;
infile 'C:\Users\tebert\Desktop\EPG SAS Manuscript\Alberto Data\Five Aphids\Data\PV4-ch5.ana' dsd dlm='09'x truncover;
input @; _infile_=compress(translate(_infile_,'.',','),'"'); input a b c ;

data one; set one; drop c; dur=0;                           insect1="a3";        
data one; set one; retain holder1 in0;
if in0 ne insect1 then do; holder1=0; in0=insect1; dur=b; end; else dur=b-holder1; holder1=b;
data two; set one; insectno=insect1; waveform=a; duration=dur;
data two; set two; drop a b holder1 in0 dur insect1;
data two; set two; retain holder1 in0;
if in0 ne insectno then do; in0=insectno; holder1=0; end; else wave1=holder1; holder1=waveform;
data two; set two; if wave1 ne "." then output; data two; set two; waveform=wave1;
data two; set two; drop in0 wave1 holder1; data two; set two; proc append base=allsets data=two;
proc datasets nolist nodetails; delete one two;

data one;
infile 'C:\Users\tebert\Desktop\EPG SAS Manuscript\Alberto Data\Five Aphids\Data\PV4-ch7.ana' dsd dlm='09'x truncover;
input @; _infile_=compress(translate(_infile_,'.',','),'"'); input a b c ;

data one; set one; drop c; dur=0;                           insect1="a4";        
data one; set one; retain holder1 in0;
if in0 ne insect1 then do; holder1=0; in0=insect1; dur=b; end; else dur=b-holder1; holder1=b;
data two; set one; insectno=insect1; waveform=a; duration=dur;
data two; set two; drop a b holder1 in0 dur insect1;
data two; set two; retain holder1 in0;
if in0 ne insectno then do; in0=insectno; holder1=0; end; else wave1=holder1; holder1=waveform;
data two; set two; if wave1 ne "." then output; data two; set two; waveform=wave1;
data two; set two; drop in0 wave1 holder1; data two; set two; proc append base=allsets data=two;
proc datasets nolist nodetails; delete one two;

data one;
infile 'C:\Users\tebert\Desktop\EPG SAS Manuscript\Alberto Data\Five Aphids\Data\PV5-ch1.ana' dsd dlm='09'x truncover;
input @; _infile_=compress(translate(_infile_,'.',','),'"'); input a b c ;

data one; set one; drop c; dur=0;                           insect1="a5";        
data one; set one; retain holder1 in0;
if in0 ne insect1 then do; holder1=0; in0=insect1; dur=b; end; else dur=b-holder1; holder1=b;
data two; set one; insectno=insect1; waveform=a; duration=dur;
data two; set two; drop a b holder1 in0 dur insect1;
data two; set two; retain holder1 in0;
if in0 ne insectno then do; in0=insectno; holder1=0; end; else wave1=holder1; holder1=waveform;
data two; set two; if wave1 ne "." then output; data two; set two; waveform=wave1;
data two; set two; drop in0 wave1 holder1; data two; set two; proc append base=allsets data=two;
proc datasets nolist nodetails; delete one two;

************************************** END *******************************************;
**************************************************************************************;








*****************************************************************************************;
**********************   Do not copy/paste anything beyond this point.             ******;
**********************                                                             ******;
**********************                                                             ******;
********************** Change the path and file name to suit your needs.           ******;
**********************                                                             ******;
**********************       If you use the same file name twice, the old version  ******;
**********************              will be lost in the process and unrecoverable. ******;
**********************       To change this feature, remove the key word "replace" ******;
*****************************************************************************************;
data allsets; set allsets;
if waveform=1 then SW="NP";
if waveform=2 then SW="C";
if waveform=3 then SW="E1e";
if waveform=4 then SW="E1";
if waveform=5 then SW="E2";
if waveform=6 then SW="F";
if waveform=7 then SW="G";
if waveform=8 then SW="PD";
if waveform=9 then SW="II2";
if waveform=10 then SW="II3";
if waveform=11 then SW="PDL";

Data allsets; set allsets; ins=insectno; wave=sw; dur=duration;
drop insectno waveform duration sw;
data allsets; set allsets; dur=round(dur,0.001);
data allsets; set allsets;
proc export data=allsets outfile='C:\Users\tebert\Desktop\EPG SAS Manuscript\Alberto Data\Five Aphids\Alberto out.csv' dbms=csv replace;

run;
quit;
